package com.transitionseverywhere;

import android.os.Build;
import android.support.v4.util.ArrayMap;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import com.transitionseverywhere.Transition;
import com.transitionseverywhere.utils.ViewGroupOverlayUtils;
import com.transitionseverywhere.utils.ViewGroupUtils;
import com.transitionseverywhere.utils.ViewUtils;
import java.util.ArrayList;
import java.util.Iterator;

/* loaded from: classes.dex */
public class TransitionManager {
    private static String LOG_TAG = "TransitionManager";
    private static Transition sDefaultTransition = new AutoTransition();
    private static final String[] EMPTY_STRINGS = new String[0];
    private static ArrayList<ViewGroup> sPendingTransitions = new ArrayList<>();
    ArrayMap<Scene, Transition> mSceneTransitions = new ArrayMap<>();
    ArrayMap<Scene, ArrayMap<Scene, Transition>> mScenePairTransitions = new ArrayMap<>();

    public void setDefaultTransition(Transition transition) {
        sDefaultTransition = transition;
    }

    public static Transition getDefaultTransition() {
        return sDefaultTransition;
    }

    public void setTransition(Scene scene, Transition transition) {
        this.mSceneTransitions.put(scene, transition);
    }

    public void setTransition(Scene scene, Scene scene2, Transition transition) {
        ArrayMap<Scene, Transition> arrayMap = this.mScenePairTransitions.get(scene2);
        if (arrayMap == null) {
            arrayMap = new ArrayMap<>();
            this.mScenePairTransitions.put(scene2, arrayMap);
        }
        arrayMap.put(scene, transition);
    }

    private Transition getTransition(Scene scene) {
        Scene currentScene;
        ArrayMap<Scene, Transition> arrayMap;
        Transition transition;
        ViewGroup sceneRoot = scene.getSceneRoot();
        if (sceneRoot == null || (currentScene = Scene.getCurrentScene(sceneRoot)) == null || (arrayMap = this.mScenePairTransitions.get(scene)) == null || (transition = arrayMap.get(currentScene)) == null) {
            Transition transition2 = this.mSceneTransitions.get(scene);
            return transition2 != null ? transition2 : sDefaultTransition;
        }
        return transition;
    }

    private static void changeScene(Scene scene, Transition transition) {
        ViewGroup sceneRoot = scene.getSceneRoot();
        if (sPendingTransitions.contains(sceneRoot)) {
            return;
        }
        Transition transition2 = null;
        if (isTransitionsAllowed()) {
            sPendingTransitions.add(sceneRoot);
            if (transition != null) {
                transition2 = transition.mo35clone();
                transition2.setSceneRoot(sceneRoot);
            }
            Scene currentScene = Scene.getCurrentScene(sceneRoot);
            if (currentScene != null && transition2 != null && currentScene.isCreatedFromLayoutResource()) {
                transition2.setCanRemoveViews(true);
            }
        }
        sceneChangeSetup(sceneRoot, transition2);
        scene.enter();
        sceneChangeRunTransition(sceneRoot, transition2);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static ArrayList<Transition> getRunningTransitions(ViewGroup viewGroup) {
        ArrayList<Transition> arrayList = (ArrayList) viewGroup.getTag(R.id.runningTransitions);
        if (arrayList == null) {
            ArrayList<Transition> arrayList2 = new ArrayList<>();
            viewGroup.setTag(R.id.runningTransitions, arrayList2);
            return arrayList2;
        }
        return arrayList;
    }

    private static void sceneChangeRunTransition(ViewGroup viewGroup, Transition transition) {
        if (transition != null && viewGroup != null && isTransitionsAllowed()) {
            ViewGroupOverlayUtils.initializeOverlay(viewGroup);
            MultiListener multiListener = new MultiListener(transition, viewGroup);
            viewGroup.addOnAttachStateChangeListener(multiListener);
            viewGroup.getViewTreeObserver().addOnPreDrawListener(multiListener);
            return;
        }
        sPendingTransitions.remove(viewGroup);
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public static class MultiListener implements ViewTreeObserver.OnPreDrawListener, View.OnAttachStateChangeListener {
        ViewGroup mSceneRoot;
        Transition mTransition;

        @Override // android.view.View.OnAttachStateChangeListener
        public void onViewAttachedToWindow(View view) {
        }

        MultiListener(Transition transition, ViewGroup viewGroup) {
            this.mTransition = transition;
            this.mSceneRoot = viewGroup;
        }

        private void removeListeners() {
            this.mSceneRoot.getViewTreeObserver().removeOnPreDrawListener(this);
            this.mSceneRoot.removeOnAttachStateChangeListener(this);
        }

        @Override // android.view.View.OnAttachStateChangeListener
        public void onViewDetachedFromWindow(View view) {
            removeListeners();
            TransitionManager.sPendingTransitions.remove(this.mSceneRoot);
            ArrayList runningTransitions = TransitionManager.getRunningTransitions(this.mSceneRoot);
            if (runningTransitions.size() > 0) {
                Iterator it = runningTransitions.iterator();
                while (it.hasNext()) {
                    ((Transition) it.next()).resume(this.mSceneRoot);
                }
            }
            this.mTransition.clearValues(true);
        }

        @Override // android.view.ViewTreeObserver.OnPreDrawListener
        public boolean onPreDraw() {
            removeListeners();
            if (TransitionManager.sPendingTransitions.remove(this.mSceneRoot)) {
                ArrayList runningTransitions = TransitionManager.getRunningTransitions(this.mSceneRoot);
                ArrayList arrayList = runningTransitions.size() > 0 ? new ArrayList(runningTransitions) : null;
                runningTransitions.add(this.mTransition);
                this.mTransition.addListener(new Transition.TransitionListenerAdapter() { // from class: com.transitionseverywhere.TransitionManager.MultiListener.1
                    @Override // com.transitionseverywhere.Transition.TransitionListenerAdapter, com.transitionseverywhere.Transition.TransitionListener
                    public void onTransitionEnd(Transition transition) {
                        TransitionManager.getRunningTransitions(MultiListener.this.mSceneRoot).remove(transition);
                    }
                });
                boolean cancelAllSystemLayoutTransitions = TransitionManager.cancelAllSystemLayoutTransitions(this.mSceneRoot);
                this.mTransition.captureValues(this.mSceneRoot, false);
                if (arrayList != null) {
                    Iterator it = arrayList.iterator();
                    while (it.hasNext()) {
                        ((Transition) it.next()).resume(this.mSceneRoot);
                    }
                }
                this.mTransition.playTransition(this.mSceneRoot);
                return !cancelAllSystemLayoutTransitions;
            }
            return true;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static boolean cancelAllSystemLayoutTransitions(View view) {
        if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            boolean cancelLayoutTransition = ViewGroupUtils.cancelLayoutTransition(viewGroup);
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                cancelLayoutTransition = cancelAllSystemLayoutTransitions(viewGroup.getChildAt(i)) || cancelLayoutTransition;
            }
            return cancelLayoutTransition;
        }
        return false;
    }

    private static void sceneChangeSetup(ViewGroup viewGroup, Transition transition) {
        if (isTransitionsAllowed()) {
            ArrayList<Transition> runningTransitions = getRunningTransitions(viewGroup);
            if (runningTransitions.size() > 0) {
                Iterator<Transition> it = runningTransitions.iterator();
                while (it.hasNext()) {
                    it.next().pause(viewGroup);
                }
            }
            if (transition != null) {
                transition.captureValues(viewGroup, true);
            }
        }
        Scene currentScene = Scene.getCurrentScene(viewGroup);
        if (currentScene != null) {
            currentScene.exit();
        }
    }

    public void transitionTo(Scene scene) {
        changeScene(scene, getTransition(scene));
    }

    public static void go(Scene scene) {
        changeScene(scene, sDefaultTransition);
    }

    public static void go(Scene scene, Transition transition) {
        changeScene(scene, transition);
    }

    public static void beginDelayedTransition(ViewGroup viewGroup) {
        beginDelayedTransition(viewGroup, null);
    }

    public static void beginDelayedTransition(ViewGroup viewGroup, Transition transition) {
        if (sPendingTransitions.contains(viewGroup) || !ViewUtils.isLaidOut(viewGroup, true)) {
            return;
        }
        sPendingTransitions.add(viewGroup);
        if (transition == null) {
            transition = sDefaultTransition;
        }
        Transition mo35clone = transition.mo35clone();
        sceneChangeSetup(viewGroup, mo35clone);
        Scene.setCurrentScene(viewGroup, null);
        sceneChangeRunTransition(viewGroup, mo35clone);
    }

    public static void endTransitions(ViewGroup viewGroup) {
        sPendingTransitions.remove(viewGroup);
        ArrayList<Transition> runningTransitions = getRunningTransitions(viewGroup);
        if (runningTransitions == null || runningTransitions.isEmpty()) {
            return;
        }
        ArrayList arrayList = new ArrayList(runningTransitions);
        for (int size = arrayList.size() - 1; size >= 0; size--) {
            ((Transition) arrayList.get(size)).end();
        }
    }

    public static boolean isTransitionsAllowed() {
        return Build.VERSION.SDK_INT >= 14;
    }

    public static void setTransitionName(View view, String str) {
        ViewUtils.setTransitionName(view, str);
    }

    public static String getTransitionName(View view) {
        return ViewUtils.getTransitionName(view);
    }
}
